package com.crape.account.bo.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.crape.account.bo.IDarrenAuditBO;
import com.crape.account.dao.IAccountDAO;
import com.crape.account.dao.IDarrenAuditDAO;
import com.crape.common.enums.DarrenstatusType;
import com.crape.common.enums.UserType;
import com.crape.common.exceptions.BOException;
import com.crape.common.model.DarrenAudit;

public class DarrenAuditBOImpl implements IDarrenAuditBO {

    private IAccountDAO     accountDAOImpl;
    private IDarrenAuditDAO darrenAuditDAOImpl;

    @Autowired
    public void setAccountDAOImpl(IAccountDAO accountDAOImpl) {
        this.accountDAOImpl = accountDAOImpl;
    }

    @Autowired
    public void setDarrenAuditDAOImpl(IDarrenAuditDAO darrenAuditDAOImpl) {
        this.darrenAuditDAOImpl = darrenAuditDAOImpl;
    }

    @Override
    public List<DarrenAudit> getDarrenAudits(DarrenAudit darrenAudit) throws BOException {
        if (darrenAudit == null) {
            return null;
        }
        return darrenAuditDAOImpl.getDarrenAudits(darrenAudit);
    }

    @Override
    public List<DarrenAudit> getPagedDarrenAudits(Long accountid, String email, Integer status, Integer startIndex,
                                                 Integer pageSize) throws BOException {
        if (status == null || startIndex == null || pageSize == null || pageSize == 0) {
            return null;
        }
        return darrenAuditDAOImpl.getPagedDarrenAudits(accountid, email, status, startIndex, pageSize);
    }

    @Override
    public Long countDarrenAudits(Long accountid, String email, Integer status) throws BOException {
        if (status == null) {
            return null;
        }
        return darrenAuditDAOImpl.countDarrenAudits(accountid, email, status);
    }

    @Override
    public void addDarrenReqest(DarrenAudit darrenAudit) throws BOException {
        if (darrenAudit == null) {
            return;
        }
        DarrenAudit params = new DarrenAudit();
        params.setAccountid(darrenAudit.getAccountid());
        List<DarrenAudit> list = darrenAuditDAOImpl.getDarrenAudits(params);
        if (list == null || list.isEmpty()) {
            darrenAuditDAOImpl.insertDarrenAudit(darrenAudit);
        } else {
            darrenAuditDAOImpl.updateDarrenAudit(darrenAudit);
        }
    }

    @Override
    public void updateDarrenAudit(DarrenAudit darrenAudit) throws BOException {
        if (darrenAudit == null) {
            return;
        }
        darrenAuditDAOImpl.updateDarrenAudit(darrenAudit);
    }

    @Override
    public void updateDarrenAuditStatus(Long accountid, Integer status) throws BOException {
        if (accountid == null || status == null) {
            return;
        }
        darrenAuditDAOImpl.updateDarrenAuditStatus(accountid, status);
        if (status == DarrenstatusType.pass.getCode()) {
            accountDAOImpl.updateDarrenStatus(accountid, UserType.darren.getCode(), status);
        } else {
            accountDAOImpl.updateDarrenStatus(accountid, UserType.user.getCode(), status);
        }
    }

}
